package com.ssm.helong.shop.controller;

import com.ssm.helong.shop.common.ServiceResultEnum;
import com.ssm.helong.shop.entity.Order;
import com.ssm.helong.shop.service.OrderService;
import com.ssm.helong.shop.util.Result;
import com.ssm.helong.shop.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 修改
    */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updateOrder(@RequestBody Order order){
        if(Objects.isNull(order.getTotalPrice())
                || Objects.isNull(order.getOrderId())
                || order.getOrderId() < 1
                || order.getTotalPrice() < 1
                || StringUtils.isEmpty(order.getReceivAddress())){
            return ResultGenerator.genFailResult("参数异常！");
        }
        String result=orderService.updateOrderInfo(order);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

    /**
     * 配货
     */

    @RequestMapping(value = "/checkDone", method = RequestMethod.POST)
    public Result checkDone(@RequestBody Integer orderId) {
        String result = orderService.checkDone(orderId);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

    /**
     * 出库
     */
    @RequestMapping(value = "/checkOut", method = RequestMethod.POST)
    @ResponseBody
    public Result checkOut(@RequestBody Integer orderId) {
        String result = orderService.checkOut(orderId);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }


    /**
     * 关闭订单
     */
    @RequestMapping(value = "/close", method = RequestMethod.POST)
    @ResponseBody
    public Result closeOrder(@RequestBody Integer orderId) {
        String result = orderService.closeOrder(orderId);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

}
